package com.example.dangerouscargodriver.ui.activity;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.baiju.netmanager.Http.IHttpRequest;
import com.baiju.style.citylist.Toast.ToastUtils;
import com.example.dangerouscargodriver.controller.AuthController;
import com.example.dangerouscargodriver.controller.UserInfoController;
import com.example.dangerouscargodriver.entry.UserInfo;
import com.example.dangerouscargodriver.module.ResponseInfo;
import com.example.dangerouscargodriver.param.RemoteAPI;
import com.example.dangerouscargodriver.param.RemoteAPICmd;
import com.example.dangerouscargodriver.utils.DialogUtils;
import com.example.dangerouscargodriver.widget.ActionBar;
import com.example.dangerouscargodriver.R;
import com.example.dangerouscargodriver.widget.ModifyPhoneNoDialog;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

public class SetupActivity extends HttpRequestActivity implements View.OnClickListener {

	private static final String TAG = "SetupActivity";
	private ActionBar mActionBar = null;
	private LinearLayout mUserInfo = null;
	private TextView mModifyTelephone = null;
	private Button mLogout = null;
	private TextView mPhoneNo = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		super.initHttpComponent(SetupActivity.class);
		setContentView(R.layout.activity_setup);

		mUserInfo = findViewById(R.id.layout_user_info);
		mUserInfo.setOnClickListener(this);

		mPhoneNo = findViewById(R.id.tv_phone_no);
		UserInfo userInfo = UserInfoController.Instance().getUserInfo();
		if(userInfo != null) {
			String maskPhone = userInfo.getMobilephone().substring(0, 3) + "****" +
					userInfo.getMobilephone().substring(7, 11);
			mPhoneNo.setText(maskPhone);
		}


		mModifyTelephone = findViewById(R.id.btn_modify_telephone);
		mModifyTelephone.setOnClickListener(this);

		mLogout = findViewById(R.id.btn_logout);
		mLogout.setOnClickListener(this);

		mActionBar = findViewById(R.id.actionBarSetup);
		mActionBar.setOnBackClickListener( new View.OnClickListener(){

			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

	@Override
	public void onClick(View v) {
		if(v.getId() == R.id.layout_user_info){
			Intent intent = new Intent(this, UserInfoActivity.class);
			startActivity(intent);
		}
		else if(v.getId() == R.id.btn_modify_telephone){
			ModifyPhoneNoDialog dialog = new ModifyPhoneNoDialog(this);
			dialog.setClickListener(new ModifyPhoneNoDialog.AlertDialogBtnClickListener() {
				@Override
				public void clickPositive(String phoneNo, String vcode) {
					executeModifyBindPhone(phoneNo, vcode);
				}

				@Override
				public void clickNegative() {

				}
			});
			dialog.showAlertDialog(true);
		}
		else if(v.getId() == R.id.btn_logout){
			AuthController.Instance().logout();
			UserInfoController.Instance().clearUserInfo();
			this.finish();
		}
		else{

		}
	}

	private void executeModifyBindPhone(String phone, String vcode){
		HashMap<String, String> header = new HashMap<String, String>();
		header.put("token", AuthController.Instance().getToken());

		HashMap<String, String> param = new HashMap<String, String>();
		param.put("mobilephone", phone);

		sendHttpRequest(IHttpRequest.RequestMethod.GET_METHOD,
				RemoteAPI.REQUEST_V1_USER_BIND_PHONE,
				header,
				param,
				RemoteAPICmd.REQUEST_V1_USER_BIND_PHONE);
	}

	@Override
	protected void onHttpResponse(int actionId, String response) {
		try {
			JSONObject res = new JSONObject(response);
			Gson gson = new Gson();
			if(actionId == RemoteAPICmd.REQUEST_V1_USER_BIND_PHONE){
				Log.d(TAG, "onResponse: " + response);
				ResponseInfo responseInfo = gson.fromJson(response, ResponseInfo.class);
				ToastUtils.showLongToast(this, res.getString("message"));
				if(responseInfo != null && responseInfo.getStatus() == 1){
					Intent intent = new Intent(this, LogInActivity.class);
					startActivity(intent);
					finish();
				}
				else{

				}
			}
			else{

			}
		}
		catch (JSONException e) {
			e.printStackTrace();
		}
	}

	@Override
	protected void onHttpErrorResponse(int actionId, String response) {

	}
}